<?php
session_start();
//phpinfo();
?>
<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>汽车之家论坛</title>
    <link href="css/slide.css" rel="stylesheet" type="text/css">
    <link rel="stylesheet" type="text/css" href="singup/css/style.css"/>
    <link rel="stylesheet" type="text/css" href="css/easydialog.css"/>
    <link rel="stylesheet" type="text/css" href="css/easyui.css">
    <link rel="stylesheet" type="text/css" href="css/icon.css">
    <link rel="stylesheet" type="text/css" href="css/main.css">
    <script type="text/javascript" charset="utf-8" src="u/ueditor.config.js"></script>
    <script type="text/javascript" charset="utf-8" src="u/ueditor.all.min.js"> </script>
    <!--建议手动加载语言，避免在ie下有时因为加载语言失败导致编辑器加载失败-->
    <!--这里加载的语言文件会覆盖你在配置项目里添加的语言类型，比如你在配置项目里配置的是英文，这里加载的中文，那最后就是中文-->
    <script type="text/javascript" charset="utf-8" src="u/lang/zh-cn/zh-cn.js"></script>
</head>
<body>
<div class="top">
    <?php include_once ("TOP.php") ?>
    <?php include_once ('INFO.php');?>
    <div class="mainContent">
        请输入标题：<input id="title"><br><br>
        <script id="editor" type="text/plain" style="width:100%;height:300px;"></script>

        <div style="margin:10px 10px 10px;"><span class="newPost" onclick="post();">发新贴</span></div>
        <div class="clear"></div>
        </div>

    <div class="clear"></div>
    <div class="bot">版权所有 汽车之家</div>
    <div id="registerForm" class="form_wrapper" style="width: 550px;">
        <form class="register">
            <h3>用户注册</h3>
            <div class="column">
                <div>
                    <label>用户名:</label>
                    <input type="text" name="username" id="username"/>
                    <span class="error">This is an error</span>
                </div>
                <div>
                    <label>昵称:</label>
                    <input type="text" name="nickname" id="nickname"/>
                    <span class="error">This is an error</span>
                </div>
                <div>
                    <label>出生日期:</label>
                    <div class="birthday"><input type="text" name="birthday" id="birthday"/>
                        <span class="error">This is an error</span></div>
                </div>
            </div>
            <div class="column">
                <div>
                    <label>选择头像:</label>
                    <input type="file" name="pic" id="pic"/>
                    <span class="error">This is an error</span>
                </div>
                <div>
                    <label>电子信箱:</label>
                    <input type="text" name="email" id="email"/>
                    <span class="error">This is an error</span>
                </div>
                <div>
                    <label>密码:</label>
                    <input type="password" name="pw" id="pw"/>
                    <span class="error">This is an error</span>
                </div>
            </div>
            <div class="bottom">
                <div class="remember"></div>
                <input type="button" value="注册" onclick="singup();"/>
                <a href="javascript:void(0);" rel="login" class="linkform" id="showLogin">已经有帐号了? 点击登录</a>
                <div class="clear"></div>
            </div>
            <div class="clear"></div>
        </form>
        <div class="clear"></div>
    </div>
    <div class="clear"></div>
    <div id="loginFrom" class="form_wrapper">
        <form class="login">
            <h3>用户登录</h3>
            <div>
                <label>用户名:</label>
                <input type="text" id="username2"/>
                <span class="error">This is an error</span>
            </div>
            <div>
                <label>密码: <a href="forgot_password.html" rel="forgot_password"
                              class="forgot linkform">忘记密码?</a></label>
                <input type="password" id="pw2"/>
                <span class="error">This is an error</span>
            </div>
            <div class="bottom">
                <div class="remember"><input type="checkbox" id="rememberMe"/><span>保存登录状态</span></div>
                <input type="button" value="登录" onclick="login();">
                <a href="javascript:void(0);" rel="register" class="linkform" id="showRegister">还没有帐号? 点击注册</a>
                <div class="clear"></div>
            </div>
        </form>
    </div>
    <div class="clear"></div>
    <script src="js/jquery.min.js"></script>
    <script src="js/jquery.form.min.js"></script>
    <script src="js/jquery.easyui.min.js"></script>
    <script src="js/responsiveslides.min.js"></script>
    <script src="js/easydialog.min.js"></script>
    <script src="js/easyui-lang-zh_CN.js"></script>
    <script src="js/site.js"></script>
    <script>
        var hasLogin = 0, hasLoggedUserName = "";
        var rememberMe = 0;
        <?php
        if(isset($_SESSION['loginUser']) and $_SESSION['loginUser'] != ""){ //说明已经处于登录状态
            echo "hasLogin = 1;";
            echo "hasLoggedUserName = '".$_SESSION['loginUser']."';";
        }
        else if(isset($_COOKIE['hasLoggedUserName']) and $_COOKIE['hasLoggedUserName'] != ""){ //说明前一次登录时，保存了登录状态
            echo "hasLogin = 1;";
            echo "hasLoggedUserName = '".$_COOKIE['hasLoggedUserName']."';";
        }
        ?>
        function post(){
            var result = getContent();
            if($("#title").val().trim() == ""){
                alert("标题必须填写！");
                return;
            }
            if(result == ""){
                alert("内容必须填写！");
                return;
            }
            $.ajax({
                url:"postMainTopic.php",
                data:{data:result,title:$("#title").val().trim()},
                dataType:"JSON",
                type:"POST",
                success:function (d) {
                    if(d.error == 1){
                        alert("发贴失败！");
                        history.back();
                    }
                    else {
                        alert("发贴成功！");
                        location.href = "index.php";
                    }
                },
                error:function (d) {
                    alert("网络错误！");
                }
            })
        }
        $(function () {
            //实例化编辑器
            //建议使用工厂方法getEditor创建和引用编辑器实例，如果在某个闭包下引用该编辑器，直接调用UE.getEditor('editor')就能拿到相关的实例
            var ue = UE.getEditor('editor');
            $("#slider").responsiveSlides({
                speed:1000
            });
            //检查是否有登录
            //有两种登录方式。
            //第1种是判断session.
            //第2种是判断cookie.
            if(hasLogin){ //说明已经登录了或者是保存了登录
                $.ajax({
                    url:"getInfo.php",
                    data:{username:hasLoggedUserName},
                    type:"post",
                    dataType:"JSON",
                    success:function (d) {
                        if(d.error ==1){
                        }
                        else{
                            $("#username1").text(d.username);
                            $("#nickname1").text(d.nickname);
                            $("#singUpDate").text(d.singUpDate);
                            $("#lastTitle").text(d.lastTitle);
                            $("#lastPostTime").text(d.lastPostTime);
                            $("#total").text(d.total);
                            $("#lastReplyTime").text(d.lastReplyTime);
                            $(".tx img").attr('src','tx/' + d.pic);
                            $(".info").show();
                            showLogout();
                        }
                    },
                    error:function (d) {
                        alert("网络超时");
                    }
                })
            }
            $("#login").click(function () {
                easyDialog.open({
                    container:'loginFrom',
                    fixed:false
                })
            })
            $("#showRegister").click(function () {
                easyDialog.close();
                easyDialog.open({
                    container:'registerForm',
                    fixed: false
                })
            })
            $("#showLogin").click(function () {
                easyDialog.close();
                easyDialog.open({
                    container:'loginFrom',
                    fixed: false
                })
            })
        })
        function getContent() {
            return UE.getEditor('editor').getContent();
        }
        function login() {
            if($("#username2").val() == "" || $("#pw2").val() == ""){
                alert("用户名和密码必须填写！");
                return;
            }
            rememberMe = $("#rememberMe").prop("checked") ? "1":"0";
            $.ajax({
                url:"postLogin.php",
                data:{
                    username:$("#username2").val(),
                    pw:$("#pw2").val(),
                    rememberMe:rememberMe},
                type:"post",
                dataType:"JSON",
                success:function (d) {
                    if(d.error ==1){
                        alert(d.errMsg);
                    }
                    else{
                        $("#username1").text(d.username);
                        $("#nickname1").text(d.nickname);
                        $("#singUpDate").text(d.singUpDate);
                        $("#lastTitle").text(d.lastTitle);
                        $("#lastPostTime").text(d.lastPostTime);
                        $("#total").text(d.total);
                        $("#lastReplyTime").text(d.lastReplyTime);
                        $("#username2").val('');
                        $("#pw2").val('');
                        $(".tx img").attr('src','tx/' + d.pic);
                        $(".info").show();
                        easyDialog.close();
                        showLogout();
                    }
                },
                error:function (d) {
                    alert("网络超时");
                }
            })
        }
    </script>
</body>
</html>